package com.ejior.bluetea.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ejior.bluetea.R;
import com.ejior.bluetea.bean.BLEDevice;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kevin on 2017/8/26.
 */

public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.ViewHolder> {

    private Context context;
    private List<BLEDevice> bleDevices = new ArrayList<>();

    public DeviceListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public DeviceListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.device_item, parent, false));
    }

    @Override
    public void onBindViewHolder(DeviceListAdapter.ViewHolder holder, int position) {
        BLEDevice bleDevice = getItem(position);
        holder.tv_device_name.setText(bleDevice.getDeviceName());
        holder.tv_device_mac.setText(bleDevice.getMac());

    }

    public void setList(List<BLEDevice> lists){
        this.bleDevices.clear();
        this.bleDevices = lists;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return bleDevices.size();
    }

    public BLEDevice getItem(int pos) {
        return bleDevices.get(pos);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_device_name;
        TextView tv_device_mac;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_device_name = (TextView) itemView.findViewById(R.id.tv_device_name);
            tv_device_mac = (TextView) itemView.findViewById(R.id.tv_device_mac);
        }
    }
}
